import { createRouter, createWebHistory } from 'vue-router'
import  useToeknStore from '@/stores/useToken'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
      // 系统用户登录
    {
      path: '/',
      name: 'login',
      component: () => import('@/views/Login.vue')
    },
  
    {
      //系统 首页
      path: '/layout',
      name: 'layout',
      component: () => import('@/layout/index.vue'),
      children: [
        // 首页头部
      {
        path: '/header',
        name: 'header',
        component: () => import('@/layout/header/index.vue')
      },
      // 首页中间显示内容
      {
        path: '/sysMain',
        name: 'sysMain',
        component: () => import('@/layout/main/index.vue'),
        children: [
          {
            path: '/sysMenuRole',
            name: 'sysMenuRole',
            component: () => import('@/layout/side/system/sysRole.vue')
          },
          {
            path: '/sysMenuUser',
            name: 'sysMenuUser',
            component: () => import('@/layout/side/system/sysUser.vue')
          },
          {
            path: '/sysMenuMenu',
            name: 'sysMenuMenu',
            component: () => import('@/layout/side/system/sysMenu.vue')
          },


          {
            path: '/sysMenuBehaviorLogs',
            name: 'sysMenuBehaviorLogs',
            component: () => import('@/layout/side/logs/behaviorLogs.vue')
          },
          {
            path: '/sysMenuControlsLogs',
            name: 'sysMenuControlsLogs',
            component: () => import('@/layout/side/logs/controlsLogs.vue')
          }
          
          ]
      
      },
      // 首页菜单
      {
        path: '/side',
        name: 'side',
        component: () => import('@/layout/side/index.vue')
// 在这个菜单中完成二级菜单的显示，以及内容展示区

      }
    ]
    },











     // 重置密码页面
    {
      path: '/resetPassword',
      name: 'resetPassword',
      component: () => import('@/views/ResetPassword.vue')
    },
    // 通配符路由，匹配所有无法识别的路径
{
  path: '/:pathMatch(.*)*',
  component: () => import('@/error/NotFount.vue')
}
 


  ]


})

// 前置守卫
// 全局拦截、除了登录页面，其他页面都必须授权（这里为pinia定义的token不为空）才能访问
router.beforeEach((to, from) => {
  const useToken=useToeknStore()
  if (to.name !== 'login' && to.name!=='resetPassword'    && !useToken.token) {

  //  alert("没有登录，自动跳转到登录页面")
    return { name: 'login' }
    
  }
  else{
    return true
  }
  }
  )




export default router
